#include<my_header.h>

static int dirs=0;
static int files=0;

void DFS(char *dirpath,int width){
    DIR* dirp=opendir(dirpath);
    ERROR_CHECK(dirp,NULL,"opendir");
    
    struct dirent* pdirent;
    while((pdirent=readdir(dirp))!=NULL){
        if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0)
           continue;
        printf("L");
        for(int i=1;i<width;++i){
            printf("-");
        }
        printf("%s\n",pdirent->d_name);
        if(pdirent->d_type==DT_DIR){
            dirs++;
            char path[1024]={0};
            sprintf(path,"%s%s%s",dirpath,"/",pdirent->d_name);
            DFS(path,width+4);
        }else{
            files++;
        }
    }
    closedir(dirp);  // 修正：将closedir移到循环外，避免在循环中重复关闭同一个目录流导致double free错误
}

int main(int argc,char* argv[]){
    ARGS_CHECK(argc,2);
    printf("%s\n",argv[1]);
    DFS(argv[1],4);
    printf("\n%d directories and %d files\n",dirs,files);
    return 0;
}
